﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Projeto.Utilidades;

namespace Projeto.Classes.Conexao
{
   public class Responsavel
    {
        //propriedades
        private SqlDataAdapter adapter; //receber o command com select e preencher datatable
        //private SqlDataReader; //receber o resultado de um command.executequery
        public DataTable dataTable; //ser preenchido pelo sqldataadapter.fill
        public Exception error; //receber a exception e possibilitar o tratamento fora da classe
        private baseConnection connection; //cria objeto base para conexão com banco
        private int rowsAffected = 0; //recebe retorno de operações com banco. Tambem utilizada como flag

        //metodos
        public bool InsertResponsavel(Objeto.Responsavel value) 
        {
            try 
            { 
                //iniciar a conexão (new connection)
                connection = new baseConnection();
                //criar a operação (command, adapter)
                connection.begin(constants.INS_RESPONSAVEL); //carreguei string, connection e sqlcommand 
                //adicionando os parametros para a procedure
                connection.addParameterToCommand(constants.PARAM_NOME, value.ds_nome);
                connection.addParameterToCommand(constants.PARAM_CNPJ_CPF, value.cd_cnpj_cpf);
                connection.addParameterToCommand(constants.PARAM_ENDERECO, value.ds_endereco);
                connection.addParameterToCommand(constants.PARAM_TELEFONE, value.ds_telefone);
                connection.addParameterToCommand(constants.PARAM_EMAIL, value.ds_email);
                connection.addParameterToCommand(constants.PARAM_TIPO_PESSOA, value.ic_tipo_pessoa);
                //abrir a conexão (connection.open)
                connection.openConnection();
                //executar a operação (command.execute... , adapter.fill)
                connection.command.ExecuteNonQuery();
                rowsAffected = 1;
            }
            catch (Exception ex) 
            {
                error = new Exception();
                error = ex;
                rowsAffected = 0;
            }
            finally
            {
                //fechar a operação (connection.close)
                if (!connection.IsClose())
                {
                    connection.closeConnection();
                }
            }

            connection = null;

            //retornar resultado da operação (success = true, fail = false)
            if (rowsAffected == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool UpdateResponsavel(Objeto.Responsavel value) 
        {
            try 
            {
                //criar a connection
                connection = new baseConnection();
                //dar o begin (carregar a string de conexão, a connection e o command)
                connection.begin(constants.UPD_RESPONSAVEL);
                //configurar parametros para o command
                connection.addParameterToCommand(constants.PARAM_ID, value.id_responsavel);
                connection.addParameterToCommand(constants.PARAM_NOME, value.ds_nome);
                connection.addParameterToCommand(constants.PARAM_CNPJ_CPF, value.cd_cnpj_cpf);
                connection.addParameterToCommand(constants.PARAM_ENDERECO, value.ds_endereco);
                connection.addParameterToCommand(constants.PARAM_TELEFONE, value.ds_telefone);
                connection.addParameterToCommand(constants.PARAM_EMAIL, value.ds_email);
                connection.addParameterToCommand(constants.PARAM_TIPO_PESSOA, value.ic_tipo_pessoa);
                //abrir a conexão
                connection.openConnection();
                //executar operação
                connection.command.ExecuteNonQuery();
                rowsAffected = 1;
            }
            catch (Exception ex) 
            {
                error = new Exception();
                error = ex;
                rowsAffected = 0;
            }
            finally 
            {
                //fechar a conexão
                if (!connection.IsClose()) 
                {
                    connection.closeConnection();
                }
            }
            
            //retornar o status da operação (success = true, fail = false)
            if (rowsAffected == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool SelectResponsavel() 
        {
            try 
            {
                //criar connection
                connection = new baseConnection();
                //iniciar connection
                connection.begin(constants.SEL_RESPONSAVEL);
                //abrir a conexão
                connection.openConnection();
                //executar a operação (no dataset)
                dataTable = new DataTable();
                adapter = new SqlDataAdapter(connection.command);
                adapter.Fill(dataTable);
                rowsAffected = 1;
            }
            catch (Exception ex) 
            {
                rowsAffected = 0;
                error = new Exception();
                error = ex;
            }
            finally 
            {
                if (!connection.IsClose()) 
                {
                    connection.closeConnection();
                }
            }

            if (rowsAffected == 1) 
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteResponsavel(Objeto.Responsavel value) 
        {
            try 
            {
                connection = new baseConnection();
                connection.begin(constants.DEL_RESPONSAVEL);
                connection.addParameterToCommand(constants.PARAM_ID, value.id_responsavel);
                connection.openConnection();
                connection.command.ExecuteNonQuery();
                rowsAffected = 1;
            }
            catch (Exception ex)
            {
                error = new Exception();
                error = ex;
                rowsAffected = 0;
            }
            finally 
            {
                if (!connection.IsClose()) 
                {
                    connection.closeConnection();
                }
            }
            if (rowsAffected == 1)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
    }
}
